package dinamica.security.setsession;

import dinamica.*;

/**
 * 设置user_id session
 * 
 * @author Martin Cordova (dinamica@martincordova.com)
 * */
public class SetUserIdSession extends GenericTransaction {

	/*
	 * (non-Javadoc)
	 * 
	 * @see dinamica.GenericTransaction#service(dinamica.Recordset)
	 */
	public int service(Recordset inputParams) throws Throwable {

		// default return code (login OK)
		int rc = 0;

		// reuse superclass code
		super.service(inputParams);

		// get db channel
		Db db = getDb();

		// verificar si el usuario es de LDAP o de DB
		String querySql = getSQL(getResource("query.sql"), inputParams);

		// crea un recordset con la data obtenida
		Recordset rsUser = db.get(querySql);

		// usa login LDAP?
		if (rsUser.getRecordCount() > 0) {
			rsUser.first();
			int user_id = rsUser.getInt("user_id");
            int role_id = rsUser.getInt("role_id");
			getSession().setAttribute("user_id", user_id);
            getSession().setAttribute("role_id", role_id);
		} else {
			getSession().setAttribute("user_id", "");
            getSession().setAttribute("role_id", "");
		}
		return rc;
	}
}
